package edu.cicese.alz;

import edu.cicese.alz.swing.JSystemFileChooser;
import edu.cicese.alz.swing.SwingUtil;
import edu.cicese.alz.util.PropertyUtil;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

/**
 * Created by: Eduardo Quintana Contreras
 * Date: 4/06/12
 * Time: 12:47 PM
 */
public class FrameSettings extends SwingComponent{

	private JTextField txtDirectory;
	private JButton btnExplore = new JButton();

	private JButton btnApply;
	private boolean isChanged = false;

	private String systemSeparator = File.separator;

	public FrameSettings() {
		super(460, 200, "Settings");
//		setMin(460, 200);
//		setInitialLocation();
//		setTitle("Settings");

		initComponents();
		setVisible(true);
	}

	private void initComponents() {
		// Pane 'Directory'
		JPanel pDirectory = new JPanel();

		// Label 'Home Directory'
		JLabel lblDirectory = new JLabel("Home directory:");
		lblDirectory.setFont(new Font("Calibri", Font.BOLD, 14));
		lblDirectory.setBorder(BorderFactory.createEmptyBorder(4, 1, 0, 1));

		// TextField 'Directory'
		txtDirectory = new JTextField();
		txtDirectory.setEditable(false);
		txtDirectory.setFont(new Font("Calibri", Font.PLAIN, 14));
		txtDirectory.setPreferredSize(new Dimension(300, 24));
		txtDirectory.setBorder(BorderFactory.createCompoundBorder(new LineBorder(new Color(212, 214, 217), 1),
				BorderFactory.createEmptyBorder(4, 1, 0, 1)));

		// get home directory from properties
		String homeDir = PropertyUtil.getProperty(PropertyUtil.HOME_DIR);
		if (homeDir != null) {
			txtDirectory.setText(homeDir);
		}

		// Button 'Select Directory'
		JButton btnExplore = new JButton();
		btnExplore.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				openFileChooser();
			}
		});

		pDirectory.add(lblDirectory);
		pDirectory.add(txtDirectory);
		addButton(btnExplore, "", 22, 24, "Explore...", "search", pDirectory);


		//TODO!
		// Database options

		// Database
		// Username
		// Password


		JButton btnOK = new JButton();
		btnOK.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				actionOK();
			}
		});
		JButton btnCancel = new JButton();
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				actionCancel();
			}
		});
		btnApply = new JButton();
		btnApply.setEnabled(isChanged);
		btnApply.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				actionApply();
			}
		});

		Box boxButtons = Box.createHorizontalBox();
		boxButtons.add(Box.createHorizontalGlue());
		addButtonBackground(btnOK, "OK", 71, 22, null, "bg", boxButtons);
		boxButtons.add(Box.createRigidArea(new Dimension(10, 30)));
		addButtonBackground(btnCancel, "Cancel", 71, 22, null, "bg", boxButtons);
		boxButtons.add(Box.createHorizontalStrut(10));
		addButtonBackground(btnApply, "Apply", 71, 22, null, "bg", boxButtons);
		boxButtons.add(Box.createHorizontalStrut(4));


		JPanel pContent = new JPanel(new BorderLayout());
		pContent.setOpaque(true);

		setContentPane(pContent);
		pContent.add(pDirectory, BorderLayout.NORTH);
		pContent.add(boxButtons, BorderLayout.SOUTH);
		pack();
	}

	private void openFileChooser() {
		JSystemFileChooser fcHomeDir = new JSystemFileChooser();
		fcHomeDir.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		fcHomeDir.setDialogTitle("Select folder");
		fcHomeDir.setApproveButtonToolTipText("Select folder");
		String dir, prevDir = txtDirectory.getText();
		fcHomeDir.setCurrentDirectory(new File(prevDir));
		if (fcHomeDir.showDialog(this, "Select") == JFileChooser.APPROVE_OPTION) {
			dir = fcHomeDir.getSelectedFile().getPath();
			txtDirectory.setText(dir);
			if (!prevDir.equals(dir)) {
				isChanged = true;
			}
		}
		enableApplyButton();
	}
	
	private void enableApplyButton() {
		btnApply.setEnabled(isChanged);
	}

	private void actionOK() {
		if (isChanged) {
			saveProperties();
		}
		//TODO reset/reload values?
		closeWindow(true);
	}

	private void actionCancel() {
		isChanged = false;
		//TODO reset/reload values?
		closeWindow(true);
	}

	private void actionApply() {
		saveProperties();
		isChanged = false;
		enableApplyButton();
	}

	private void saveProperties() {
		// save home directory
		PropertyUtil.setProperty(PropertyUtil.HOME_DIR, txtDirectory.getText());
	}



	public static void main(String[] a) {
		SwingUtil.setUIManager();
		new FrameSettings();
	}
}
